package com.attendance.www.hr.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.attendance.www.hr.beans.YearFurloughBean;
import com.attendance.www.hr.dao.mapper.YearFurloughMapper;
import com.attendance.www.hr.entitys.YearFurlough;
import com.attendance.www.hr.entitys.YearFurloughExample;
import com.attendance.www.hr.exception.ResultFaultException;
import com.attendance.www.hr.service.IYearFurloughService;

@Service
public class YearFurloughServiceImpl implements IYearFurloughService {
	
	@Resource
	private YearFurloughMapper yearFurloughDao;
	
	
	//验证是否已有该年（增and改）
	private void checkYear(YearFurloughBean yearFurloughBean) {
		int year = yearFurloughBean.getYear();
		YearFurloughExample example=new YearFurloughExample();
		example.createCriteria().andYearEqualTo(year);
		List<YearFurlough> yfList = yearFurloughDao.selectByExample(example);
		if (yfList.size()>0) {
			throw new ResultFaultException("14_00001");
		}
	}
	
	/**
	 * 增
	 */
	@Override
	public boolean saveYearFurlough(YearFurloughBean yearFurloughBean) {
		this.checkYear(yearFurloughBean);
		YearFurlough yearFurlough = new YearFurlough();
		yearFurlough.setMonth(yearFurloughBean.getMonth());
		yearFurlough.setYear(yearFurloughBean.getYear());
		int record=yearFurloughDao.insertSelective(yearFurlough);
		if (record <= 0) {
			return false;
		}
		return true;
	}

	/**
	 * 删
	 */
	@Override
	public boolean deleteYearFurlough(String yearFurloughIdStr) {
		int yearFurloughId=Integer.valueOf(yearFurloughIdStr);
		yearFurloughDao.deleteByPrimaryKey(yearFurloughId);
		return true;
	}

	/**
	 * 改
	 */
	@Override
	public boolean updateYearFurlough(YearFurloughBean yearFurloughBean) {
		int yearFurloughId=yearFurloughBean.getYearFurloughId();
		YearFurlough yearFurloughDB = yearFurloughDao.selectByPrimaryKey(yearFurloughId);
		//如果已被修改，返回false
		if (!yearFurloughBean.getCreateTime().equals(yearFurloughDB.getCreateTime())) {
			return false;
		}
		//如果修改了year
		if (!yearFurloughDB.getYear().equals(yearFurloughBean.getYear())) {
			this.checkYear(yearFurloughBean);
			yearFurloughDB.setYear(yearFurloughBean.getYear());
		}
		yearFurloughDB.setMonth(yearFurloughBean.getMonth());
		int record=yearFurloughDao.updateByPrimaryKeySelective(yearFurloughDB);
		if (record <= 0) {
			return false;
		}
		return true;
	}

	/**
	 * 查
	 */
	@Override
	public List<YearFurloughBean> getYearFurloughList() {
		List<YearFurloughBean> yearFurloughBeanList = new ArrayList<>();
		List<YearFurlough> yearFurloughList = yearFurloughDao.selectByExample(new YearFurloughExample());
		for (YearFurlough yearFurlough : yearFurloughList) {
			YearFurloughBean yearFurloughBean = new YearFurloughBean();
			yearFurloughBean.setCreateTime(yearFurlough.getCreateTime());
			yearFurloughBean.setMonth(yearFurlough.getMonth());
			yearFurloughBean.setUpdateTime(yearFurlough.getUpdateTime());
			yearFurloughBean.setYear(yearFurlough.getYear());
			yearFurloughBean.setYearFurloughId(yearFurlough.getYearFurloughId());
			yearFurloughBeanList.add(yearFurloughBean);
		}
		return yearFurloughBeanList;
	}

	/**
	 * 验证该时间是否可用去年年假
	 */
	@Override
	public boolean getCheckYearFurlough(String yearStr, String monthStr) {
		// TODO Auto-generated method stub
		int year = Integer.valueOf(yearStr)-1;
		int month = Integer.valueOf(monthStr);
		YearFurloughExample example=new YearFurloughExample();
		example.createCriteria().andYearEqualTo(year);
		List<YearFurlough> yearFurloughList = yearFurloughDao.selectByExample(example);
		if (yearFurloughList.size()>0) {
			int monthDB = yearFurloughList.get(0).getMonth();
			if (monthDB >= month) {
				return true;
			}
		}
		return false;
	}

}
